﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WeChatRobotSDKCommon.Helper;
using WeChatRobotSDKDao.DB;
using WeChatRobotSDKModel.TableModel;
using WeChatRobotSDKServer;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WeChatRobotSDKApi.Controllers.Job
{
    [Route("[controller]/[action]")]
    public class JobController : Controller
    {
        private MysqlDbContext dbContext;
        public WeChatRobotServer _weChatRobotServer;
        public StrProcessingServer _strProcessingServer;
        public JobController(MysqlDbContext _dbContext, WeChatRobotServer weChatRobotServer, StrProcessingServer strProcessingServer)
        {
            this.dbContext = _dbContext;
            _weChatRobotServer = weChatRobotServer;
            _strProcessingServer = strProcessingServer;
        }

        public string SendMsg()
        {

            var data = dbContext.Set<Weibopp>().Where(c => c.IsPush == 0 && c.YouHuiType == 0).ToList();

            foreach (var item in data)
            {

                //string imagename = RemoteImageCatchUtil.Catch(item.ImageUrl, "Image");
                //string imagepath = $"D:\\代码\\我的代码\\3、WeChatHook\\WeChatExt\\WeChatRobotApi\\WeChatRobotSDKApi\\Image\\{imagename}";
                //string imagepath = "";

                string msg = _strProcessingServer.StrProcessing(item);
                string wxid = "luoguoqi123";//luoguoqi123  onlyyou0730mm
                _weChatRobotServer.SendTextMessage(wxid, msg);

                if (!string.IsNullOrEmpty(item.ImageUrl))
                {
                    foreach (var itemUrl in item.ImageUrl.Split('-'))
                    {
                        if (string.IsNullOrEmpty(itemUrl))
                        {
                            continue;
                        }
                        string imagename = RemoteImageCatchUtil.Catch(itemUrl, "Image");
                        string imagepath = $"D:\\代码\\我的代码\\3、WeChatHook\\WeChatExt\\WeChatRobotApi\\WeChatRobotSDKApi\\Image\\{imagename}";
                        _weChatRobotServer.SendImageMsg(wxid, imagepath);
                    }
                }
                item.IsPush = 1;
                dbContext.Set<Weibopp>().Update(item);
                dbContext.SaveChanges();
                Thread.Sleep(3000);
            }
            return "发送成功";

        }
    }
}
